package com.ruoyi.web.web.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.User;
import com.ruoyi.accountant.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user, HttpSession seesion) {
        User u = userService.findByLoginNameAndUpass(user.getLoginName(), user.getUpass());
        seesion.setAttribute("login_user", u);
        AjaxResult ajax = AjaxResult.success();
        ajax.put(Constants.TOKEN,seesion.getAttribute("login_user"));
        return ajax;
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login.html";
    }
}
